<template>
  <div ref="echartDom"></div>
</template>

<script setup>
import {ref, defineProps, onMounted, onUnmounted, inject, shallowRef, defineExpose} from 'vue'
import {pxToRem} from "@/utils/index";

const props = defineProps({
  lineData: {
    type: Object,
    default: () => {
    }
  },
  dialogData: {
    type: Object,
    default: () => ({
      fontSizeTimes: 1
    })
  }
});

const echartDom = ref(null);
const myChart = shallowRef(null);
const echarts = inject('echarts');

const initEchart = () => {
  if (echartDom.value) {
    myChart.value = echarts.init(echartDom.value);
    let tempSeriesData = props?.lineData?.seriesData?.map((x, i) => {
      return {
        data: x,
        type: 'line',
        stack: props?.lineData?.legendData?.[i],
        name: props?.lineData?.legendData?.[i],
        smooth: false,
        showSymbol: false,
      }
    })
    tempSeriesData.push({
      data: [],
      type: 'line',
      markArea: {
        silent: true,
        itemStyle: {
          color: 'rgba(87,69,140,0.5)',
        },
        label: {
          color: 'white',
          fontSize: pxToRem(12 * props?.dialogData?.fontSizeTimes),
          position: 'inside'
        },
        data: [
          [
            {
              name: '预测',
              xAxis: props?.lineData?.forecastIndex
            },
            {
              xAxis: props?.lineData?.xAxisData?.length - 1
            }
          ]
        ]
      },
    })
    let option = {
      tooltip: {
        trigger: 'axis',
        formatter: (val) => {
          let doms = ''
          let tempMarker = val[0].marker
          tempMarker = tempMarker.replace('#5470c6', '#e31919')
          val.forEach((x, i) => {
            doms += `${x.marker + x.seriesName}：${x.value}万㎡` + '<br>'
          })
          doms = doms + `${tempMarker}净差值：${val[0].value - val[1].value}万㎡` + '<br>'
          return (
            `${val[0].name}` + '<br>' + doms
          )
        }
      },
      legend: {
        x: 'center',
        icon: "rect",
        top: `${props?.dialogData?.fontSizeTimes === 1 ? 7 : 0}%`,
        itemWidth: pxToRem(10 * props?.dialogData?.fontSizeTimes),
        itemHeight: pxToRem(10 * props?.dialogData?.fontSizeTimes),
        textStyle: {
          color: 'white',
          fontSize: pxToRem(12 * props?.dialogData?.fontSizeTimes),
          padding: [pxToRem(2), 0, 0],
          rich: {}
        },
        tooltip: {
          show: true
        },
        formatter: (params) => {
          let val = '';
          if (params.length > 5) {
            val = `${params.substr(0, 5)}...`;
            return val;
          } else {
            return params;
          }
        },
        data: props?.lineData?.legendData
      },
      grid: {
        left: '2%',
        right: '5%',
        bottom: '5%',
        top: `${props?.dialogData?.fontSizeTimes === 1 ? 17 : 20}%`,
        containLabel: true
      },
      xAxis: {
        min: 0,
        type: 'category',
        boundaryGap: false,
        data: props?.lineData?.xAxisData,
        axisLabel: {
          fontSize: pxToRem(9 * props?.dialogData?.fontSizeTimes),
          color: 'white',
          fontWeight: 'lighter'
        },
        axisLine: {
          show: true // 不显示坐标轴线
        }
      },
      yAxis: {
        min: 0,
        max: (value) => value.max || 200,
        name: '万㎡',
        nameTextStyle: {
          color: 'white',
          fontSize: pxToRem(12 * props?.dialogData?.fontSizeTimes),
          align: 'left',
          padding: [0, 0, 0, pxToRem(-15 * props?.dialogData?.fontSizeTimes)]
        },
        nameGap: pxToRem(props?.dialogData?.fontSizeTimes === 1 ? 15 : 35),
        tooltip: {
          show: true
        },
        axisTick: {
          show: false // 不显示坐标轴刻度线
        },
        axisLine: {
          show: true, // 不显示坐标轴线
        },
        splitLine: {
          show: false // 不显示网格线
        },
        axisLabel: {
          fontSize: pxToRem(9 * props?.dialogData?.fontSizeTimes),
          color: 'white'
        }
      },
      series: tempSeriesData,
    }
    option && myChart.value.setOption(option, true);

    myChart.value.on("legendselectchanged", (params) => {
      event.stopPropagation();
    })
  }
}

const updateChart = () => {
  if (myChart.value) {
    myChart.value.dispose(); // 销毁旧图表实例
  }
  initEchart();
};

defineExpose({
  updateChart
})

onMounted(() => {
  initEchart();
  window.addEventListener("resize", updateChart);
})

onUnmounted(() => {
  window.removeEventListener("resize", updateChart);
})
</script>

<style scoped lang="less">
.h250 {
  height: 250px;
}

.dialog-h250 {
  width: 1120px;
  height: 500px;
}
</style>